<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" v-if="isHide">
      <el-form-item label="文件名称" prop="moduleName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入文件名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="isHide">
        <el-upload
          class="upload-demo"
          :action="upload.uploadUrl"
          :headers="upload.headers"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          :limit="1"
          :on-exceed="handleExceed"
          :on-success="handleAvatarSuccess"
          :show-file-list="false"
          ref="upload"
          :file-list="upload.imageList"
          >
          <el-button size="mini" type="primary" @click="uploadFile"><i class="el-icon-upload el-icon--right"/>上传</el-button>
        </el-upload>
      </el-col>
      <el-col :span="1.5" v-if="isHide">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ghxx:upload:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-check"
          size="mini"
          :disabled="single"
          @click="handleChoice"
        >选择</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ghxx:upload:export']"
          v-if="false"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="uploadList" @selection-change="handleSelectionChange" highlight-current-row empty-text="暂无数据">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id唯一标识" align="center" prop="id" v-if="false"/>
      <el-table-column label="文件名称" align="center" prop="fileName" show-overflow-tooltip v-if="isHide"/>
      <el-table-column label="上传人" align="center" prop="updateBy" v-if="isHide"/>
      <el-table-column label="上传时间" align="center" prop="updateTime"/>
      <el-table-column label="图标展示" align="center" prop="readData" v-if="isHideImage">
            <template slot-scope="scope">
              <img :src="baseApi+scope.row.resourceUrl" style="width:30px;height:30px;">
          </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="isHide">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleLook(scope.row)"
            v-if="false"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleDownload(scope.row)"
          >下载</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ghxx:upload:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      title="图标展示"
      :visible.sync="dialogVisible"
      width="21%">
       <img :src="imageUrl" class="image">
    </el-dialog>
  </div>
</template>

<script>
import { listUpload, getUpload, delUpload, addUpload, updateUpload, exportUpload } from "@/api/upload/upload";
import { getToken } from "@/utils/auth";
export default {
  name: "Upload",
   props: {
    moduleName: String,
    message:String,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 上传信息表格数据
      uploadList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptIdStrs: null,
        moduleName: "参数图标",
        resourceUrl: null,
        orderByColumn:"id",
        isAsc:"desc"
      },
      upload: {
     // 设置上传的请求头部
      headers: { Authorization: "Bearer " + getToken() },
      // 上传的地址
      uploadUrl:"",
      imageList:[],
      },
      dialogVisible:false,
      baseApi:process.env.VUE_APP_BASE_API,
      imageUrl:'',
      selectionImage:{},
      isHide:true,
      isHideImage:true,
    }
  },
  created() {
    console.log(this.moduleName);
    this.getList();
  },
  mounted(){
    if(this.message!=undefined){
      this.isHide=false;
      this.isHideImage=false;
      this.isHideImage=true;
    }
  },
  methods: {
    handleChoice(){
      console.log("选择图标",this.selectionImage);
      this.$parent.$parent.handleChoice(this.selectionImage);
    },
    /** 查询上传信息列表 */
    getList() {
      this.loading = true;
      listUpload(this.queryParams).then(response => {
        this.uploadList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
      this.selectionImage=selection;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除上传信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUpload(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
       /** 查看按钮操作 */
    handleLook(row) {
      this.dialogVisible=true;
      this.imageUrl=process.env.VUE_APP_BASE_API +row.resourceUrl;
      // this.$confirm('是否确认查看此文件', "警告", {
      //     confirmButtonText: "确定",
      //     cancelButtonText: "取消",
      //     type: "warning"
      //   }).then(function() {
      //   window.open(process.env.VUE_APP_BASE_API +row.resourceUrl)
      //   })
    },
      /** 下载按钮操作 */
    handleDownload(row) {
      this.$confirm('是否确认下载此文件', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
        let link = document.createElement('a')
        link.style.display = 'none'
        link.href = process.env.VUE_APP_BASE_API +row.resourceUrl;
        link.setAttribute('download', "文件名称");
        document.body.appendChild(link)  ;
        link.click();
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有上传信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUpload(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    uploadFile(){
      if(this.moduleName==undefined||this.moduleName==''||this.moduleName==null){
          this.moduleName="参数图标"
      }
      this.upload.uploadUrl=process.env.VUE_APP_BASE_API+"/ghxx/upload/uploadData"+"?moduleName="+this.moduleName;
    },
    handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      handleExceed(files, fileList) {
        console.log(files)
        console.log(fileList)
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
      handleAvatarSuccess(res, file){
      if(res.code==200){
        this.msgSuccess("上传成功");
        this.getList();
        this.$refs.upload.clearFiles()
      }else{
        this.msgSuccess(res.msg);
      }
    },
  }
};
</script>
<style>
/* .app-container .el-upload-list {
    margin: 0;
    padding: 0;
    list-style: none;
    display: none;
} */
  .image {
    width:30px;
    height: 30px;
  }
</style>>
